#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QMessageBox>
#include<QPainter>
#include"weatherto.h"
#include<QTimer>
#define INCREMENT 3 //温度每升高/降低 1°y坐标的增量
#define POINT_RADIUS 2 //曲线描点的大小
#define TEXT_OFFSET_X 12
#define TEXT_OFFSET_Y 12
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(856,580);
    setWindowFlag(Qt::FramelessWindowHint);
    mybtn=new mybutton(":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/search(1).png");

    connect(mybtn,&mybutton::clicked,this,[=](){
       mybtn->zoom1();
        mybtn->zoom2();
        QTimer::singleShot(350,this,[=](){
            QString cityname=ui->citysearch->text();
            getweatherInof(cityname);});
        });
    mybtn->setParent(this);
    mybtn->size();
    mybtn->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(0, 0, 0, 0);" // 设置背景为透明，最后一个数字是透明度 (0-1之间)
        "border: none;" // 如果不需要边框可以添加这个
        "padding: 10px;" // 默认内边距
        "}"
        );
    mybtn->setCursor(Qt::PointingHandCursor);
    QSizePolicy policy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    policy.setHorizontalStretch(0.1);
    policy.setVerticalStretch(0.1);
    mybtn->setSizePolicy(policy);

    mybtn->move(200,20);
    exitmenu=new QMenu(this);
    exitact=new QAction();
    NetworkManager=new QNetworkAccessManager(this);
    exitact->setText("退出");
    exitact->setIcon(QIcon(":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/close.png"));
    exitmenu->addAction(exitact);

    connect(exitact,&QAction::triggered,this,&MainWindow::close);
    connect(NetworkManager,&QNetworkAccessManager::finished,this,&MainWindow::onreplied);
    //北京城市编码
    //getweatherInof("101010100");
    //将控件添加到控件数组
    mywWeekist<<ui->day0<<ui->day1<<ui->day2<<ui->day3<<ui->day4<<ui->day5;
    mytypeIconList<<ui->type1<<ui->type2<<ui->type3<<ui->type4<<ui->type5<<ui->type6;

    mytypeList<<ui->degre1_2<<ui->degre2_2<<ui->degre3_2<<ui->degre4_2<<ui->degre5_2<<ui->degre6_2;
    myAqiList<<ui->degre1<<ui->degre2<<ui->degre3<<ui->degre4<<ui->degre5<<ui->degre6;


    myFlList<<ui->feng1<<ui->feng2<<ui->feng3<<ui->feng4<<ui->feng5<<ui->feng6;

    mtype.insert("暴雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/BaoXue.png");
    mtype.insert("暴雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/BaoYu.png");
    mtype.insert("暴雨到大暴雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/BaoYuDaoDaBaoYu.png");
    mtype.insert("大暴雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/DaBaoYu.png");
    mtype.insert("大到暴雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/DaDaoBaoXue.png");
    mtype.insert("大到暴雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/DaDaoBaoYu.png");
    mtype.insert("大雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/DaXue.png");
    mtype.insert("大雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/DaYu.png");
    mtype.insert("冻雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/DongYu.png");
    mtype.insert("多云",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/DuoYun.png");
    mtype.insert("浮沉",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/FuChen.png");
    mtype.insert("雷阵雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/LeiZhenYu.png");
    mtype.insert("雷阵雨伴冰雹",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/LeiZhenYuBanYouBingBao.png");
    mtype.insert("霾",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/Mai.png");
    mtype.insert("强沙尘暴",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/QiangShaChenBao.png");
    mtype.insert("晴",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/Qing.png");
    mtype.insert("沙尘暴",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/ShaChenBao.png");
    mtype.insert("特大暴雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/TeDaBaoYu.png");
    mtype.insert("undefined",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/undefined.png");
    mtype.insert("雾",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/Wu.png");
    mtype.insert("小到中雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/XiaoDaoZhongXue.png");
    mtype.insert("小到中雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/XiaoDaoZhongYu.png");
    mtype.insert("小雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/XiaoXue.png");
    mtype.insert("小雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/XiaoYu.png");
    mtype.insert("雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/Xue.png");
    mtype.insert("扬沙",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/YangSha.png");
    mtype.insert("阴",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/Yin.png");
    mtype.insert("雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/Yu.png");
    mtype.insert("雨夹雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/YuJiaXue.png");
    mtype.insert("阵雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/ZhenXue.png");
    mtype.insert("阵雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/ZhenYu.png");
    mtype.insert("中到大雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/ZhongDaoDaXue.png");
    mtype.insert("中到大雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/ZhongDaoDaYu.png");
    mtype.insert("中雪",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/ZhongXue.png");
    mtype.insert("中雨",":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/type/ZhongYu.png");

    ui->message1->setPixmap(QPixmap(":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/wind.png"));
    ui->message1->setScaledContents(true);
    ui->message5->setPixmap(QPixmap(":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/humidity.png"));
    ui->message5->setScaledContents(true);
    ui->message3->setPixmap(QPixmap(":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/pm25.png"));
    ui->message3->setScaledContents(true);
    ui->message7->setPixmap(QPixmap(":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/aqi.png"));
    ui->message7->setScaledContents(true);

    //给标签加入事件过滤器

    ui->label_3->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::contextMenuEvent(QContextMenuEvent* event)
{
    exitmenu->exec(QCursor::pos());

}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    moffset= event->globalPos()-this->pos();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos()-moffset);
}

void MainWindow::onreplied(QNetworkReply *reply)
{
    int statuscode=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    qDebug()<<reply->operation();
    qDebug()<<statuscode;
    if(statuscode!=200||reply->error()!=QNetworkReply::NoError)
    {
        QMessageBox::warning(this,"天气","请求数据失败");
    }else
    {
        QByteArray byteArray=reply->readAll();
        qDebug()<<""<<byteArray.data();
        parsejson(byteArray);
    }
    reply->deleteLater();

}

void MainWindow::getweatherInof(QString cityname)
{
    QString cityCode=weather::getcitycode(cityname);

    if(cityCode.isEmpty())
    {
        QMessageBox::warning(this,"天气","请检查输入是否正确");
        return;
    }
    QUrl url("http://t.weather.itboy.net/api/weather/city/"+cityCode);
    NetworkManager->get(QNetworkRequest(url));
}

void MainWindow::parsejson(QByteArray ayyay)
{
   QJsonParseError err;
   QJsonDocument doc= QJsonDocument::fromJson(ayyay,&err);
    if(err.error!=QJsonParseError::NoError)
    {
       return;
    }else
    {
        QJsonObject rootobj=doc.object();
        qDebug()<<rootobj.value("message").toString();

        //解析日期和城市
       mTody.date= rootobj.value("date").toString();
       mTody.city= rootobj.value("cityInfo").toObject().value("city").toString();

       //解析yesterday
       QJsonObject objData=rootobj.value("data").toObject();
       QJsonObject objyesterday= objData.value("yesterday").toObject();
       mDay[0].week=objyesterday.value("week").toString();
       mDay[0].date=objyesterday.value("ymd").toString();
       mDay[0].type=objyesterday.value("type").toString();

       QString highS;
       highS = objyesterday.value("high").toString().split(" ").at(1);
       mDay[0].high = highS.left(highS.length() - 1).toInt();

       QString lowS;
       lowS = objyesterday.value("low").toString().split(" ").at(1);
       mDay[0].low = lowS.left(lowS.length() - 1).toInt();

       //风向 风力
       mDay[0].fx=objyesterday.value("fx").toString();
       mDay[0].fl=objyesterday.value("fl").toString();

       //污染指数
       mDay[0].aqi=objyesterday.value("aqi").toDouble();

       //解析forcase
       QJsonArray forecastarr=objData.value("forecast").toArray();
       for(int i=0;i<5;i++)
       {
           QJsonObject objforcase= forecastarr[i].toObject();
           mDay[i+1].week=objforcase.value("week").toString();
           mDay[i+1].type=objforcase.value("type").toString();
           mDay[i+1].date=objforcase.value("ymd").toString();
           mDay[i+1].fl=objforcase.value("fl").toString();
           mDay[i+1].fx=objforcase.value("fx").toString();

           QString highS;
           highS = objforcase.value("high").toString().split(" ").at(1);
           mDay[i+1].high = highS.left(highS.length() - 1).toInt();
           QString lowS;
           lowS = objforcase.value("low").toString().split(" ").at(1);
           mDay[i+1].low = lowS.left(lowS.length() - 1).toInt();
       }
           //今天
           objData.value("ganmao");
           mTody.ganmao=objData.value("ganmao").toString();
           mTody.wendu=objData.value("wendu").toString();
           mTody.shidu=objData.value("shidu").toString();
           mTody.pm25=objData.value("pm25").toDouble();
           mTody.quality=objData.value("quality").toString();

           mTody.type=mDay[1].type;
           mTody.fx=mDay[1].fx;
           mTody.fl=mDay[1].fl;
           mTody.high=mDay[1].high;
           mTody.low=mDay[1].low;

            //更新ui

            updateUi();
    }
}

void MainWindow::updateUi()
{
    //日期和城市
    ui->day0_2->setText(QDateTime::fromString(mTody.date,"yyyyMMdd").toString("yyyy/MM/dd")+" "+mDay[1].week);
    ui->label_2->setText(mTody.city);
    //今天
    ui->weather->setPixmap(QPixmap(mtype[mTody.type]));

    ui->wendu->setText(mTody.wendu+"°C");

    QString str=mTody.type+QString::number(mTody.low)+"~"+QString::number(mTody.high)+"°C";
    qDebug()<<mTody.low;
    ui->lineEdit->setText(str);
    ui->textEdit->setText("感冒指数:"+mTody.ganmao);

    str.clear();
    str=mTody.fx+"\n"+"   "+mTody.fl;
    ui->message2->setText(str);

    str.clear();
    str = QString("pm2.5\n   %1").arg(QString::number(mTody.pm25));
    ui->message4->setText(str);

    str.clear();
    str=QString("湿度\n %1").arg(mTody.shidu);
    ui->message6->setText(str);

    str.clear();
    str=QString("空气质量\n    %1").arg(mTody.quality);
    ui->message8->setText(str);
    //六天
    for(int i=0;i<6;i++)
    {
        QStringList ymdlist= mDay[i].date.split("-");
        qDebug()<<mDay[i].date;
        QString str=ymdlist[1]+"/"+ymdlist[2];
        if(i==0)
        {
            mywWeekist[i]->setText(QString::fromUtf8("昨天") + "\n" + str);
            qDebug()<<QString::fromUtf8("昨天") + "\n" + str;
            continue;
        }
        if(i==1)
        {
            mywWeekist[i]->setText(QString::fromUtf8("今天") + "\n" + str);
            continue;
        }
        if(i==2)
        {
            mywWeekist[i]->setText(QString::fromUtf8("明天") + "\n" + str);
            continue;
        }
        if(i!=0&&i!=2&&i!=1)
        {mywWeekist[i]->setText("周"+mDay[i].week.right(1)+"\n"+str);}


    }
    for(int i=0;i<6;i++)
    {
        mytypeIconList[i]->setPixmap(QPixmap(mtype[mDay[i].type]));
        //风力/风向
        myFlList[i]->setText(mDay[i].fx+"\n"+mDay[i].fl);

        mytypeList[i]->setText("   "+mDay[i].type);
        if(mDay[i].aqi>=0&&mDay[i].aqi<=50)
        {
            myAqiList[i]->setText("   优");
            myAqiList[i]->setStyleSheet("background-color:rgb(121,184,0);");
        }else if(mDay[i].aqi>50&mDay[i].aqi<=100)
        {
            myAqiList[i]->setText("   良");
            myAqiList[i]->setStyleSheet("background-color:rgb(255,187,23);");
        }
        else  if(mDay[i].aqi>100&mDay[i].aqi<=150)
        {
            myAqiList[i]->setText("   轻度");
            myAqiList[i]->setStyleSheet("background-color:rgb(255,87,97);");
        }
        else if(mDay[i].aqi>150&mDay[i].aqi<=200)
        {
            myAqiList[i]->setText("   中度");
            myAqiList[i]->setStyleSheet("background-color:rgb(235,17,27);");
        }
        else if(mDay[i].aqi>200&mDay[i].aqi<=250)
        {
            myAqiList[i]->setText("   重度");
            myAqiList[i]->setStyleSheet("background-color:rgb(170,0,0);");
        }else
        {
            myAqiList[i]->setText("   严重");
            myAqiList[i]->setStyleSheet("background-color:rgb(110,0,0);");
        }
    }

}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPixmap pix;
    pix.load(":/C:/Users/20644/Desktop/Qt/weather/11-新建工程+右键菜单/res/background.png");
    QPainter mypainter(this);
    mypainter.drawPixmap(0,0,this->width(),this->height(),pix);
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==ui->label_3&&event->type()==QEvent::Paint)
    {
        painthighcurce();
    }
    return QMainWindow::eventFilter(watched,event);
}

void MainWindow::painthighcurce()
{
    QPainter painter(ui->label_3);
    //抗锯齿
    painter.setRenderHint(QPainter::Antialiasing,true);
    //获取x坐标
    int pointX[6]={0};
    for(int i=0;i<6;i++)
    {
        pointX[i]=mywWeekist[i]->pos().x()+mywWeekist[i]->width()/2;
    }
    //获取y坐标
    int tempsum=0;
    int tempaverage=0;
    for(int i=0;i<6;i++)
    {
        tempsum+=mDay[i].high;
    }
    //最高温平均值
    tempaverage=tempsum/6;

    int pointy[6]={0};
    int ycenter=ui->label_3->height()/2;
    for(int i=0;i<6;i++)
    {
        pointy[i]=ycenter-((mDay[i].high-tempaverage)*INCREMENT);
    }

    //绘制
    QPen pen=painter.pen();
    pen.setWidth(1);
    pen.setColor(QColor(255,150,50));

    painter.setPen(pen);
    painter.setBrush(QColor(255,150,50));
    for(int i=0;i<6;i++)
    {
        painter.drawEllipse(QPoint(pointX[i],pointy[i]),POINT_RADIUS,POINT_RADIUS);
        painter.drawText(pointX[i]-TEXT_OFFSET_X,pointy[i]-TEXT_OFFSET_Y,QString::number(mDay[i].high)+"°");
    }
    //绘制曲线
    for(int i=0;i<5;i++)
    {
        if(i==0)
        {
            pen.setStyle(Qt::DotLine);
            painter.setPen(pen);
        }else
        {
            pen.setStyle(Qt::SolidLine);
            painter.setPen(pen);
        }
        painter.drawLine(pointX[i],pointy[i],pointX[i+1],pointy[i+1]);
    }
    //低温
    //获取x坐标

    pointX[6]={0};
    for(int i=0;i<6;i++)
    {
        pointX[i]=mywWeekist[i]->pos().x()+mywWeekist[i]->width()/2;
    }
    //获取y坐标
    pointy[6]={0};
    for(int i=0;i<6;i++)
    {
        pointy[i]=ycenter-((mDay[i].low-tempaverage)*INCREMENT);
    }

    //绘制
    pen.setWidth(1);
    pen.setColor(QColor(155,240,50));

    painter.setPen(pen);
    painter.setBrush(QColor(155,240,50));
    for(int i=0;i<6;i++)
    {
        painter.drawEllipse(QPoint(pointX[i],pointy[i]),POINT_RADIUS,POINT_RADIUS);
        painter.drawText(pointX[i]-TEXT_OFFSET_X,pointy[i]-TEXT_OFFSET_Y,QString::number(mDay[i].low)+"°");
    }
    //绘制曲线
    for(int i=0;i<5;i++)
    {
        if(i==0)
        {
            pen.setStyle(Qt::DotLine);
            painter.setPen(pen);
        }else
        {
            pen.setStyle(Qt::SolidLine);
            painter.setPen(pen);
        }
        painter.drawLine(pointX[i],pointy[i],pointX[i+1],pointy[i+1]);
    }
    update();
}




